<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script type="text/javascript">
        window.onload=function()
        {
            //let和const都是块级作用域，但是const声明的变量不允许被修改
            let x=null;
            //获取元素对象
            const btn1=document.getElementById("btn1");
            const btn2=document.getElementById("btn2");
            btn1.onclick=function()
            {
                x=new XMLHttpRequest();
                x.open("GET","http://127.0.0.1:8000/delay");
                x.send();
            };
            //取消请求
            btn2.onclick=function()
            {
                x.abort();
            };
        };
    </script>
</head>
<body>
    <button id="btn1">点击发送</button>
    <button id="btn2">点击取消</button>
</body>
</html>